const network = require('../../utils/network.js');
const jiayouMgr = require('../../store/jiayouStore.js')
var observer = require('../../libs/observer').observer;

Page(observer({

  /**
   * 页面的初始数据
   */
  props:{
    store: jiayouMgr.store
  },
  data: {
    //面值
    values:[],
  // 充值卡列表
    GasCards:[],
    show:false,
    selectGasCard:null,
    activeValue:null,
    OrderInfoString:'尾号3636中国石油300充值卡'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadCardList();
    jiayouMgr.list(list=>{
      this.setData({
        GasCards:list,
        selectGasCard:list[0]
      });
      jiayouMgr.store.activeGasCard = list[0];
    });
  },

  loadCardList: function () {
    network.requestLoading('mp/service/gas/rechargeGasList', {}, '请求中', (res) => {
      this.setData({
        values: res.data
      })
    })
  },

  getOrderInfoString: function (item){
    const suffixCardNum = jiayouMgr.store.activeGasCard.cardNum.slice(-6,-1) ;
    const gasCardStr = jiayouMgr.store.activeGasCard.gasCardType === 'zhongshiyou' ?'中国石油':'中国石化'
    return '尾号' + suffixCardNum + gasCardStr + item.rechargeAmount/100+'元充值卡'
  },

  showConfirm:function(e){
    if (jiayouMgr.store.activeGasCard==null){
      this.goCardList();
      return;
    }
    const item = e.currentTarget.dataset.item ;
    this.setData({
      show:true,
      activeValue: item,
      OrderInfoString: this.getOrderInfoString(item)
    })
  },

  onClose:function(){
    this.setData({
      show: false
    })
  },

  coinCharge:function(){
    const params = {
      "shopId": this.data.activeValue.shopId,
      "productId": this.data.activeValue.id,
      "orderCategory": "recharge_youka",
      "businessId": jiayouMgr.store.activeGasCard.id
    }
    jiayouMgr.createGasShopOrder(params,(res)=>{
      this.onClose();
      const payOrderNo = res.data.orderNo;
      this.confirmOrder(payOrderNo);
    });
  },

  goCardList:function(){
    wx.navigateTo({
      url: '/pages/jiayou/gasCardList'
    })
  },

  // 提交订单
  confirmOrder: function (orderNo) {
    const parmas = {
      "openId": "",
      "payChannel": "payByIntegral",
      "payOrderNo": orderNo
    }
    network.requestLoading('mp/client/keplerPay/confirm', parmas, '请求中', (res) => {
      const payOrderNo = res.data.payOrderNo;
      wx.navigateTo({
        url: '/pages/pay-result/cardCharge-result?payOrderNo=' + payOrderNo,
      })
    })
  }
}))